package cn.msn.system.service.impl;

import cn.msn.system.domain.Role;
import cn.msn.system.mapper.RoleMapper;
import cn.msn.system.service.IRoleService;
import cn.msn.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void save(Role role) {
        super.save(role);
        // 添加当前角色权限
        roleMapper.addRolePermission(role);
        // 添加角色菜单
        roleMapper.addRoleMenu(role);

    }

    @Override
    @Transactional
    public void remove(Long id) {

        // 根据角色ID删除权限
        roleMapper.delPermissionByRoleId(id);
        // 根据角色ID删除菜单
        roleMapper.delMenuByRoleId(id);
        // 根据角色ID删除员工
        roleMapper.delEmployeeByRoleId(id);
        super.remove(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        // 修改：先删除再添加
        roleMapper.delPermissionByRoleId(role.getId());
        roleMapper.delMenuByRoleId(role.getId());

        // 添加当前角色权限
        roleMapper.addRolePermission(role);
        // 添加角色菜单
        roleMapper.addRoleMenu(role);
    }
}
